package top.lgd.utils;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.date.TemporalUtil;
import lombok.extern.slf4j.Slf4j;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * @author YYJ
 * @version 1.0
 * @since 2020/12/10 15:03
 */
@Slf4j
public class DateUtils extends LocalDateTimeUtil {

    public final static String YYYY_MM = "yyyy-MM";
    public final static String YYYY = "yyyy";
    public final static String YYYY_MM_DD = "yyyy-MM-dd";
    public final static String MM_DD = "MM-dd";

    public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 转为LocalDateTime
     *
     * @param dateTime
     * @return
     */
    public static LocalDateTime toLocalDateTime(String dateTime) {
        try {
            return parse(dateTime, DEFAULT_FORMAT);
        } catch (Exception e) {
            log.error("> {}时间转换出错了：", dateTime, e);
        }
        return null;
    }

    public static Duration between(LocalDate startDate, LocalDate endDate) {
        return TemporalUtil.between(startDate, endDate);
    }

    public static long between(LocalDate startDate, LocalDate endDate, ChronoUnit unit) {
        return unit.between(startDate, endDate);
    }

    public static String today() {
        return LocalDate.now().toString();
    }

    /**
     * 根据日获取账单日期
     *
     * @param day
     * @return
     */
    public static LocalDate getBillDate(String day) {
        LocalDate localDate = LocalDate.now();
        if (StringUtils.isNotEmpty(day)) {
            localDate = parseDate(getMonth() + "-" + day);
        }
        if (LocalDate.now().isAfter(localDate)) {
            localDate = localDate.plus(1, ChronoUnit.MONTHS);
        }
        return localDate;
    }

    /**
     * 获取月份
     *
     * @return 获取月份 YYYY-MM
     */
    public static String getMonth(LocalDate localDate) {
        try {
            return format(localDate, YYYY_MM);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取月份
     *
     * @return 获取上个月份 YYYY-MM
     */
    public static String getMonth() {
        try {
            return format(LocalDate.now(), YYYY_MM);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String getYear(LocalDate localDate) {
        try {
            return format(localDate, YYYY);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String toDateString(LocalDate localDate) {
        return format(localDate, YYYY_MM_DD);
    }

    public static String toShortDate(LocalDate localDate) {
        return format(localDate, MM_DD);
    }


    /**
     * 获取上个月份
     *
     * @return 获取上个月份 YYYY-MM
     */
    public static String getLastMonth() {
        try {
            LocalDate localDate = LocalDate.now();
            return format(localDate.minus(1, ChronoUnit.MONTHS), YYYY_MM);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
